package com.example.demo.controller;

import com.example.demo.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

//使用注解不必实例化对象
@Slf4j
@RestController
@RequestMapping("/api/params")
public class ParamsController {

//    响应体一般会根据返回的类型自动处理，文本text/plain,对象则是application/json

//    路径变量PathVariable
    @GetMapping("/path/{id}")
    public Long getParams(@PathVariable("id") Long id) {
        return id;
    }

//    路径上?key=value通过RequestParam，post方法传递的参数也用这种方式
    @GetMapping("/requestParams")
    public String requestParams(@RequestParam("name") String name, @RequestParam(value = "age", defaultValue = "0") int age) {
        log.info("test");
        return "Name: " + name + ", Age: " + age;
    }

// @RequestBody可以post传递过来的参数对象直接映射到实体类
    @PostMapping("/post")
    public User getParams(@RequestBody User user) {
        return user;
    }

//    自定义响应体类型
    @PutMapping("/selfDefine")
    public ResponseEntity<User> selfDefine(@RequestBody User user) {
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }

    // 获取文件使用RequestParam，RequestPart
    @PostMapping("/file")
    public String uploadFile(@RequestBody User user,@RequestParam("file") MultipartFile file) {
        return "123456";
    }


}
